package ai.peanut.selfbase.modules.office.service.impl;

import ai.peanut.selfbase.common.domain.DataResult;
import ai.peanut.selfbase.common.utils.UserUtils;
import ai.peanut.selfbase.modules.office.dao.OfficeMapper;
import ai.peanut.selfbase.modules.office.entity.Office;
import ai.peanut.selfbase.modules.office.pojo.OfficeParams;
import ai.peanut.selfbase.modules.office.service.IOfficeService;
import ai.peanut.selfbase.modules.sys.pojo.ZTreeDto;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 机构表 服务实现类
 * </p>
 *
 * @author lichong
 * @since 2019-02-15
 */
@Service
public class OfficeServiceImpl extends ServiceImpl<OfficeMapper, Office> implements IOfficeService {




    /**
     * 查找组织列表  ztree类型
     * @return
     */
    @Override
    public List<ZTreeDto> selectOfficeZTreeListChooseOfficeId(Long officeId){
        List<ZTreeDto> ztreeList = new ArrayList<>() ;
        OfficeParams params = new OfficeParams() ;
        params.setDelFlag( 1 ) ;
        List<Office> listAllOffice = baseMapper.selectAllByParams( params ) ;
        if( listAllOffice == null || listAllOffice.size() == 0 ) {
            return ztreeList ;
        }
        for( Office office : listAllOffice ) {
            ZTreeDto zTreeDto = new ZTreeDto();
            zTreeDto.setId(office.getId());
            zTreeDto.setParentId(office.getParentId());
            zTreeDto.setName(office.getName());
            zTreeDto.setOpen(false); //目录
            zTreeDto.setChecked(false);
            if(officeId != null && office.getId().intValue() == officeId.intValue() ) {
                zTreeDto.setChecked(true);
            }
            ztreeList.add(zTreeDto) ;
        }

        return ztreeList ;
    }

    /**
     * 获取列表
     * @param param
     * @return
     */
    @Override
    public List<Office> selectAllByParams(OfficeParams param) {
        return  baseMapper.selectAllByParams( param) ;
    }



    /**
     * 更新菜单
     * @param params
     * @return
     */
    @Override
    public DataResult<Boolean> updateOffice(OfficeParams params){
        Date now = new Date() ;
        Office Office = new Office() ;
        BeanUtils.copyProperties(params,Office);
        Long userId = UserUtils.getCurrentUser().getId() ;
        Office.setUpdateUserId(userId);
        Office.setUpdateTime(now);
        Office.setCreateTime(null);
        Office.setCreateUserId(null);
        Office.setDelFlag(null);
        this.generatorIds(Office);
        baseMapper.updateById(Office);
        return DataResult.success() ;
    }
    /**
     * 新增菜单
     * @param params
     * @return
     */
    @Override
    public DataResult<Boolean> insertOffice(OfficeParams params){

            Date now = new Date();
            Office Office = new Office();
            BeanUtils.copyProperties(params, Office);
            Long userId = UserUtils.getCurrentUser().getId();
            Office.setCreateUserId(userId);
            Office.setUpdateUserId(userId);
            Office.setCreateTime(now);
            Office.setUpdateTime(now);
            Office.setDelFlag(1);
            this.generatorIds(Office);
            baseMapper.insertAllColumn(Office);

            Integer.parseInt("etretre");


        return DataResult.success() ;
    }

    private void generatorIds(Office Office ){
        if( Office.getParentId() == null || Office.getParentId().intValue() == 0 ){
            Office.setParentId(0L);
            Office.setParentIds("0,"+Office.getId());
        }else {
            Office m = baseMapper.selectById( Office.getParentId() ) ;
            Office.setParentIds( m.getParentIds() + ","+Office.getId());
        }
    }

    /**
     * 删除
     * @param
     * @return
     */
    @Override
    public int deleteCurrentAndChildren(OfficeParams params){
        params.setUpdateTime(new Date());
        params.setParentIds(null);
        int num = baseMapper.deleteCurrentAndChildren(params);
        if(params.getDelFlag() == 0 ){
            Office Office = baseMapper.selectById( params.getId() ) ;
            params.setId(null);
            params.setParentIds( Office.getParentIds() ) ;
            num =+ baseMapper.deleteCurrentAndChildren( params ) ;
        }
        return num ;
    }

}
